package com.example.a10928.weifriend.ui.task_center;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a10928.weifriend.App;
import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.databinding.FragmentTaskCenterBinding;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.ui.MainActivity;
import com.example.a10928.weifriend.ui.home.menu.NoticeActivity;
import com.example.a10928.weifriend.ui.home.menu.UpdateVIPActivity;
import com.example.a10928.weifriend.util.L;
import com.example.a10928.weifriend.util.RxUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static com.example.a10928.weifriend.App.app;

public class TaskCenterFragment extends BaseFragment {

    private FragmentTaskCenterBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_task_center, container, false);
        mBinding.setView(this);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.taskTvInform.setSelected(true);



        mBinding.btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mBinding.taskSubsidyState.getText().equals("已领取")){
                    L.t("今日已领取过");
                }else{
                    App.app().getBpService().get_everyday_bonus(App.app().getUser().getUserId())
                            .compose(RxUtil.io2main())
                            .subscribe(
                                    r->{
                                        L.t("获取补贴成功");
                                    }
                                    ,
                                    RxUtil::errorException
                            );
                }

            }
        });

        isGettedBonus();

    }

    public void isGettedBonus(){
        App.app().getBpService().check_everyday_bonus(App.app().getUser().getUserId(),App.app().getUser().getToken())
                .compose(RxUtil.io2main())
                .subscribe(
                        r->{
                            mBinding.taskSubsidyState.setText(r.getMsg());

                        },
                        RxUtil::errorException
                );
    }


    public void onViewClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.task_vip_select:
                intent.putExtra(MemberTaskListActivity.Companion.getSOURCE_MEMBERTASKLISTACTIVITY_TYPE(), MemberTaskListActivity.Companion.getSOURCE_TASK_VIP());
                intent.setClass(getContext(), MemberTaskListActivity.class);
                startActivity(intent);
                break;
            case R.id.task_common_select:
                intent.putExtra(MemberTaskListActivity.Companion.getSOURCE_MEMBERTASKLISTACTIVITY_TYPE(), MemberTaskListActivity.Companion.getSOURCE_TASK_COMMON());
                intent.setClass(getContext(), MemberTaskListActivity.class);
                startActivity(intent);
                break;
            case R.id.task_submit_select:
                intent.setClass(getContext(), TaskRecordActivity.class);
                intent.putExtra(VariableName.DATA,"3");
                startActivity(intent);
                break;
            case R.id.task_tv_inform:
                intent.setClass(getContext(), NoticeActivity.class);
                startActivity(intent);
                break;
            case R.id.task_banner:
                intent.setClass(getContext(), UpdateVIPActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getNewNotice();
    }

    @SuppressLint("CheckResult")
    public void getNewNotice() {
        ((MainActivity) getActivity()).showProgress();
        app().getBpService().getNewNotice()
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            ((MainActivity) getActivity()).dismmProgress();
                            mBinding.taskTvInform.setText(r.getData().getNotice_content());
                        }, RxUtil::errorException
                );

        ((MainActivity) getActivity()).showProgress();
        app().getBpService().getAllBanner("2")
                .compose(RxUtil.io2main())
                .subscribe(
                        r -> {
                            Picasso.with(getContext()).load(r.getData().get(0).getBanner_imgurl()).into(mBinding.taskBanner);
                        }, RxUtil::errorException
                );
    }
}
